package com.yygo.model;

import javax.persistence.*;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * Created by tiansha on 2015/8/5.
 */
@Entity
@Table(name = "bond_company")
public class BondCompany extends BaseObject {

    private static final long serialVersionUID = 8549297932718475490L;
    private Long id;
    private String name;
    private boolean autoAudit;
    private Set<LoanProduct> loanProducts = new HashSet<>();
    private Set<Long> productIds = new HashSet<>();
    private Set<Dealer> dealers = new HashSet<>();

    private User bondUser;
    private BondCompany parent;
  private Long parentId;
    private List<BondCompany> children;

    private boolean slaveProvider;
    private boolean masterProvider;

    private Set<Dealer> slaveDealers;
    private Set<Dealer> masterDealers;

    private String bank;
    private String bankAccount;
    private String cardName;

    public BondCompany() {}

    public BondCompany(String name) {
        this.name = name;
    }

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    @Column(nullable = false)
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Column(name="auto_audit")
    public boolean isAutoAudit() {
        return autoAudit;
    }

    public void setAutoAudit(boolean autoAudit) {
        this.autoAudit = autoAudit;
    }

    @ManyToMany(mappedBy = "bonds")
    public Set<LoanProduct> getLoanProducts() {
        return loanProducts;
    }

    public void setLoanProducts(Set<LoanProduct> loanProducts) {
        this.loanProducts = loanProducts;
    }

  @Transient
  public Set<Long> getProductIds() {
    return productIds;
  }

  public void setProductIds(Set<Long> productIds) {
    this.productIds = productIds;
  }

    @OneToMany(mappedBy = "bond", cascade=CascadeType.ALL)
    public Set<Dealer> getDealers() {
        return dealers;
    }

    public void setDealers(Set<Dealer> dealers) {
        this.dealers = dealers;
    }

    @OneToOne
    @JoinColumn(name="bond_user_id")
    public User getBondUser() {
        return bondUser;
    }

    public void setBondUser(User bondUser) {
        this.bondUser = bondUser;
    }

    @ManyToOne
    @JoinColumn(name="parent_id")
    public BondCompany getParent() {
        return parent;
    }

    public void setParent(BondCompany parent) {
        this.parent = parent;
    }

    @OneToMany(mappedBy = "parent", cascade = CascadeType.REMOVE)
    public List<BondCompany> getChildren() {
        return children;
    }

  @Transient
  public Long getParentId() {
    return parentId;
  }

  public void setParentId(Long parentId) {
    this.parentId = parentId;
  }
    public void setChildren(List<BondCompany> children) {
        this.children = children;
    }

    @Column(name="slave_provider")
    public boolean isSlaveProvider() {
        return slaveProvider;
    }

    public void setSlaveProvider(boolean slaveProvider) {
        this.slaveProvider = slaveProvider;
    }

    @Column(name="master_provider")
    public boolean isMasterProvider() {
        return masterProvider;
    }

    public void setMasterProvider(boolean masterProvider) {
        this.masterProvider = masterProvider;
    }

    @OneToMany(mappedBy = "slaveProvider")
    public Set<Dealer> getSlaveDealers() {
        return slaveDealers;
    }

    public void setSlaveDealers(Set<Dealer> slaveDealers) {
        this.slaveDealers = slaveDealers;
    }

    @OneToMany(mappedBy = "masterProvider")
    public Set<Dealer> getMasterDealers() {
        return masterDealers;
    }

    public void setMasterDealers(Set<Dealer> masterDealers) {
        this.masterDealers = masterDealers;
    }

    @Column(name="bank")
    public String getBank() {
        return bank;
    }

    public void setBank(String bank) {
        this.bank = bank;
    }

    @Column(name="bank_account")
    public String getBankAccount() {
        return bankAccount;
    }

    public void setBankAccount(String bankAccount) {
        this.bankAccount = bankAccount;
    }

    @Column(name="card_name")
    public String getCardName() {
        return cardName;
    }

    public void setCardName(String cardName) {
        this.cardName = cardName;
    }


    @Override
    public String toString() {
        return "Bond: " + name;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (!(o instanceof Bank)) {
            return false;
        }

        final BondCompany bond = (BondCompany) o;

        return !(name != null ? !name.equals(bond.getName()) : bond.getName() != null);
    }

    @Override
    public int hashCode() {
        return (name != null ? name.hashCode() : 0);
    }
}
